
$("#upload").change(async function () {
    let input = $(this)[0];
    if (input.files.length > 0) {
        loading = layer.open({
            type:1,
            title: false,
            content: $(".load"),
            closeBtn:false,
            btn:false
        })
        for (let i = 0; i < input.files.length; i++) {
            let fileItem = input.files[i];
            let file = new FormData()
            $("#current").text(i+1);
            $("#allNumber").text(input.files.length)
            $("#upName").text(fileItem.name)
            $("#upSize").text((fileItem.size/1024/1024).toFixed(2)+"MB")
            layui.element.progress('filter-progress', Math.ceil(((i+1)/input.files.length)*100)+'%')
            file.append('file', fileItem)
            file.append("folder",folder)
            file.append("filename",fileItem.name)
            await new Promise((resolve,reject)=>{
                $.ajax({
                    url: "/index/upload/image",
                    method: 'post',
                    data: file,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        if(res.code!==200){
                            layer.msg(res.msg)
                        }
                        resolve(res);
                    },
                    error:function(){
                        resolve(true);
                    }
                })
            })
        }
        location.reload()
        layer.close(loading)
    }
})
if ($.cookie('copyFile')) {
    $("#parseThis").show();
}

$(".backLast").click(function () {
    window.history.back()
})
$(".folder-list-img").click(function () {
    layer.photos({
        photos: ".folder-list",
        anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
    });
})

$(".next-page").click(function () {
    page += 1;
    location.replace('/index/manager/folder?' + insertParam('page', page));
})
$(".last-page").click(function () {
    page -= 1;
    if (page <= 0) {
        page = 0
    }
    location.replace('/index/manager/folder?' + insertParam('page', page));
})
$("#allSetting").click(function () {
    $(".container-check").show();
    $("#offSetting").show();
    $(".listCheck").each((e, d) => {
        d.checked = false
    })
    $(".allc").show()
    $(this).hide();
})

function closeOff() {
    $("#selectAll").text("全选")
    $(".container-check").hide();
    $("#allSetting").show();
    $(".allc").hide()
    $("#offSetting").hide();
}

$("#offSetting").click(closeOff)

function all() {

}

function listen() {
    $(".folder-list").on("contextmenu", function (e) {
        closeMouse()
        chooseId = $(this).attr("data-id");
        const {clientX, clientY} = e;
        $("#mouseRight").css({
            top: clientY,
            left: clientX,
            display: 'block'
        })
        e.preventDefault();
        e.stopPropagation();
    })
}

listen();
$("#removeFile").click(function () {
    let del = chooseId;
    $.ajax({
        url: '/index/manager/del',
        method: 'post',
        data: {id: del},
        success: (result) => {
            layer.msg(result.msg)
            if (result.code === 1) {
                $(`[data-id='${del}']`).remove();
            }
        }
    })
})
$("#downloadFile").click(function () {
    let url = $(`[data-id='${chooseId}']`).attr('data-url')
    window.open(url)
})
$("#copyUrl>ul>li").click(function () {
    let url = $(`[data-id='${chooseId}']`).attr('data-url')
    switch ($(this).attr("data-type")) {
        case "url":
            break;
        case "md":
            url = `![img](${url})`;
            break;
        case "html":
            url = `<img src="${url}" alt="" />`;
            break;
        case "BBCode":
            url = `[img]${url}[/img]`
            break
    }
    copyText(url)
    layer.msg("复制成功")
})
$("#copyAllUrl").click(function () {
    let files = [];
    $(".listCheck").each((e, d) => {
        if (d.checked) {
            let url = $(`[data-id='${$(d).attr('data-ids')}']`).attr('data-url')
            files.push(url);
        }
    })
    let str = files.join("\n");
    copyText(str);
    closeOff()
    layer.msg("复制成功")
})
$("#delAll").click(function () {
    let files = [];
    $(".listCheck").each((e, d) => {
        if (d.checked) {
            files.push($(d).attr("data-ids"));
        }
    })
    if (files.length > 0) {
        layer.confirm(`确定删除已选中的文件？`, {
            icon: 2,
            title: '警告'
        }, function (index) {
            $.ajax({
                url: '/index/manager/del',
                method: 'post',
                data: {id: files.join(',')},
                success: (result) => {
                    layer.msg(result.msg)
                    if (result.code === 1) {
                        location.reload();
                    }
                }
            })
        });
    }
})
$("#copy").click(function () {
    $.cookie("copyFile", chooseId);
    layer.msg("剪切成功,请粘贴到其他文件夹吧！")
})
$("#selectAll").click(function () {
    let s = $("#selectAll").text();
    $(".listCheck").each((e, d) => {
        if (s === '全选') {
            d.checked = true
        } else {
            d.checked = false
        }
    })
    $("#selectAll").text(s === '全选' ? '取消全选' : '全选')
})
$("#copyAll").click(function () {
    let files = [];
    $(".listCheck").each((e, d) => {
        if (d.checked) {
            files.push($(d).attr("data-ids"));
        }
    })
    if (files.length > 0) {
        $.cookie("copyFile", files.join(","));
        layer.msg("剪切成功,请粘贴到其他文件夹吧！")
    }
})
$("#parseThis").click(function () {
    let a = $.cookie("copyFile");
    if (a) {
        $.ajax({
            url: '/index/manager/move',
            method: 'post',
            data: {id: a, folder: folder},
            success: (result) => {
                layer.msg(result.msg)
                if (result.code === 1) {
                    $.removeCookie("copyFile")
                    let item = result.data.info;
                    location.reload();
                }
            }
        })
    }
})
const closeMouse = () => {
    $("#mouseRight").css({
        display: 'none'
    })
}
document.body.addEventListener("click", () => {
    closeMouse()
})